﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Mnuemonix {
   public class DefinitionScreen : GameScreen {
        private Texture2D backGround;
        private ScreenSize screenSize;
        private List<Word> wordList; //TODO: Let GameScreen read the wordList and the subClasses inherit it
        private static int screenCount = 0;

        private TextBox wordText;
        SpriteFont gameFont;
        SpriteFont textFont;

        public DefinitionScreen(Microsoft.Xna.Framework.Game game, ScreenSize screenSize, List<Word> wordList)
            : base(game)
        {
            this.wordList = wordList;
            this.screenSize = screenSize;
        }

        public override void LoadContent()
        {
            BUTTON_HEIGHT = screenSize.ScreenWidth / 18; //fix
            BUTTON_WIDTH = screenSize.ScreenWidth / 9; //fix
            int x = screenSize.ScreenWidth / 3 - BUTTON_WIDTH / 2; //fix
            int y = Convert.ToInt32(screenSize.ScreenHeight * .9); //fix
            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {
                button_state[i] = BState.UP;
                button_color[i] = Color.White;
                button_timer[i] = 0.0;
                button_rectangle[i] = new Rectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
                x += BUTTON_WIDTH + Convert.ToInt32(screenSize.ScreenWidth * .1); //fix
            }

            this.backGround = this.content.Load<Texture2D>("background");

            button_texture[PREV_INDEX] = this.content.Load<Texture2D>("prev");
            button_texture[ABOUT_INDEX] = this.content.Load<Texture2D>("about");
            button_texture[NEXT_INDEX] = this.content.Load<Texture2D>("next");

            gameFont = content.Load<SpriteFont>("GameFont");
            textFont = content.Load<SpriteFont>("textFont");
            wordText = new TextBox(this, new Vector2(Convert.ToInt32(screenSize.ScreenWidth * .22), Convert.ToInt32(screenSize.ScreenHeight * .345)),
                new Vector2(Convert.ToInt32(screenSize.ScreenWidth - screenSize.ScreenWidth * .32), Convert.ToInt32(screenSize.ScreenHeight - screenSize.ScreenHeight * .48)), "textFont", wordList[screenCount].definition, true);
        }

        public override void Draw(GameTime gameTime)
        {
            this.screenManager.batch.Begin();

            this.screenManager.batch.Draw(this.backGround, new Rectangle(0, 0, screenSize.ScreenWidth, screenSize.ScreenHeight), Color.White);
            //TODO: use gamescreens.count and do away with screenCount
            //TODO: Should draw the picFrame around picture with the same dimensions
            this.screenManager.batch.Draw(this.content.Load<Texture2D>(wordList[screenCount].word), new Rectangle(Convert.ToInt32(screenSize.ScreenWidth * .22), Convert.ToInt32(screenSize.ScreenHeight * .245), 
                Convert.ToInt32(screenSize.ScreenWidth - screenSize.ScreenWidth * .32), Convert.ToInt32(screenSize.ScreenHeight - screenSize.ScreenHeight*.48)), Color.White);
            //TODO: Scale font depending on the size of the screen
            this.screenManager.batch.DrawString(gameFont, wordList[screenCount].word, new Vector2(Convert.ToInt32(screenSize.ScreenWidth * .4), 0), Color.Black);

            //TODO: THIS WORKS NOW USE FOR DEFINITION
            this.wordText.Draw(this.screenManager.batch);

            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {
                this.screenManager.batch.Draw(button_texture[i], button_rectangle[i], button_color[i]);
            }

            this.screenManager.batch.End();
        }

        public override void Update(GameTime gameTime)
        {
            wordText = new TextBox(this, new Vector2(Convert.ToInt32(screenSize.ScreenWidth * .22), Convert.ToInt32(screenSize.ScreenHeight * .12)),
                new Vector2(Convert.ToInt32(screenSize.ScreenWidth - screenSize.ScreenWidth * .32), Convert.ToInt32(screenSize.ScreenHeight - screenSize.ScreenHeight * .48)), "textFont", wordList[screenCount].definition, true);
            // get elapsed frame time in seconds
            frame_time = gameTime.ElapsedGameTime.Milliseconds / 1000.0;

            // update mouse variables
            MouseState mouse_state = Mouse.GetState();
            mx = mouse_state.X;
            my = mouse_state.Y;
            prev_mpressed = mpressed;
            mpressed = mouse_state.LeftButton == ButtonState.Pressed;

            update_buttons();
        }

        // Logic for each button click goes here
        public override void take_action_on_button(int i)
        {
            //take action corresponding to which button was clicked
            switch (i)
            {
                case PREV_INDEX:
                    screenCount--;
                    this.screenManager.removeScreen(this);
                    break;
                case ABOUT_INDEX:
                    this.screenManager.addScreen(new AboutScreen(this.screenManager.Game, new ScreenSize(screenSize.ScreenHeight, screenSize.ScreenWidth), wordList));
                    break;
                case NEXT_INDEX:
                    //background_color = Color.Red;               
                    this.screenManager.addScreen(new DefinitionScreen(this.screenManager.Game, new ScreenSize(screenSize.ScreenHeight, screenSize.ScreenWidth), wordList));
                    screenCount++;
                    break;
                default:
                    break;
            }
        }

    }
}
